package fhp.hlhj.giantfold.fragment

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.MainActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.activity.login.LoginAty
import fhp.hlhj.giantfold.activity.login.WechatLoginAty
import fhp.hlhj.giantfold.activity.message.MessageAty
import fhp.hlhj.giantfold.activity.user.BindPhoneAty
import fhp.hlhj.giantfold.activity.user.UserCenter
import fhp.hlhj.giantfold.adapter.SpendVPAdp
import fhp.hlhj.giantfold.customView.FlipDialog
import fhp.hlhj.giantfold.customView.LoadingDialog
import fhp.hlhj.giantfold.customView.MyHeader
import fhp.hlhj.giantfold.interfaces.IAdv
import fhp.hlhj.giantfold.interfaces.ILunbo
import fhp.hlhj.giantfold.interfaces.IUser
import fhp.hlhj.giantfold.javaBean.AdvBean
import fhp.hlhj.giantfold.javaBean.LunBoBean
import fhp.hlhj.giantfold.javaBean.PopBean
import fhp.hlhj.giantfold.javaBean.UserInfoBean
import fhp.hlhj.giantfold.network.Urls
import fhp.hlhj.giantfold.presenter.AdvPresenter
import fhp.hlhj.giantfold.presenter.LunboPresenter
import fhp.hlhj.giantfold.presenter.UserPresenter
import fhp.hlhj.giantfold.utils.LoadingUtils
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.MyUtils
import fhp.hlhj.giantfold.utils.SPUtils
import kotlinx.android.synthetic.main.fgm_spendmoney.view.*
import java.util.ArrayList

/**
 * author: FHP
 * data: on 2017\11\17 0017 16:16
 * E-Mail: 103945089@qq.com
 */
class SpendFgm :Fragment(), SpringView.OnFreshListener,IUser,IAdv, ILunbo {



    override fun onLoadmore() {
    }
    override fun onRefresh() {
        val httpParams = HttpParams()
        httpParams.put("user_id",Contents.userId)
        userPresneter.getUser(httpParams)

    }


    private lateinit var userPresneter:UserPresenter
    private lateinit var advPresenter:AdvPresenter
    private lateinit var lunboPresenter:LunboPresenter
    private var viewPager: ViewPager? = null
    private lateinit var rootView: View
    private lateinit var pageAdp:SpendVPAdp
    private var lunboDatas=ArrayList<LunBoBean.DataBean>()
    private lateinit var loadingDialog:LoadingDialog
    private lateinit var spendBuyFgm:Fragment
    private lateinit var spendLocalFgm:Fragment
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val inflate = inflater.inflate(R.layout.fgm_spendmoney, null)
        viewPager=inflate.viewPager
        rootView=inflate
        loadingDialog=LoadingUtils.getInstence(context)
        userPresneter =UserPresenter(this)
        lunboPresenter= LunboPresenter(this)
        advPresenter= AdvPresenter(this)


        LogUtil.log("走一次创建方法")

        setPager()
        setSpView()
        initListener()
        setFgm()
        return inflate
    }

    /**
     *使能板块
     */
    private fun setFgm() {
        spendBuyFgm=SpendBuyFgm()
        spendLocalFgm=SpendLocalFgm()
        MyUtils.trans(activity.supportFragmentManager,R.id.spendFrame,spendBuyFgm)
    }

    /**
     * 信息更新
     */
    override fun onResume() {
        super.onResume()
        /**
         * 根据登录类型判断
         */
        LogUtil.logi("登录类型${SPUtils.readLoginType(context)}userId=${Contents.userId}")
        when(SPUtils.readLoginType(context)){
            -1->{
                rootView.btSelf.setOnClickListener{
                    context.startActivity(Intent(context, WechatLoginAty::class.java))
                }
                Glide.with(context).load(R.mipmap.placeholdericon).asBitmap().dontAnimate().into(rootView.btSelf)
            }
            else->{
                if (Contents.userId.equals("0")){
                    rootView.btSelf.setOnClickListener{
                        context.startActivity(Intent(context, BindPhoneAty::class.java))
                    }
                }else{
                    val httpParams = HttpParams()
                    httpParams.put("user_id",Contents.userId)
                    userPresneter.getUser(httpParams)
                    rootView.btSelf.setOnClickListener{
                        context.startActivity(Intent(context, UserCenter::class.java))
                    }
                }

            }
        }
    }
    private fun initListener() {

        rootView.btBuyFgm.setOnClickListener {
            if (Contents.userId.equals("0")){
                val intent =Intent(activity, WechatLoginAty::class.java)
                activity.startActivityFromFragment(this,intent,1)
            }else{
                rootView.btBuyFgm.setBackgroundColor(Color.WHITE)
                rootView.btBuyFgm.setTextColor(Color.rgb(255,103,66))
                rootView.btLocalFgm.setBackgroundColor(Color.rgb(250,250,250))
                rootView.btLocalFgm.setTextColor(Color.rgb(102,102,102))
                MyUtils.trans(activity.supportFragmentManager,R.id.spendFrame,spendBuyFgm)
            }

        }
        rootView.btLocalFgm.setOnClickListener {
            if (Contents.userId.equals("0")){
                val intent =Intent(activity, WechatLoginAty::class.java)
                activity.startActivityFromFragment(this,intent,1)
            }else{
                rootView.btLocalFgm.setBackgroundColor(Color.WHITE)
                rootView.btLocalFgm.setTextColor(Color.rgb(255,103,66))
                rootView.btBuyFgm.setBackgroundColor(Color.rgb(250,250,250))
                rootView.btBuyFgm.setTextColor(Color.rgb(102,102,102))
                MyUtils.trans(activity.supportFragmentManager,R.id.spendFrame,spendLocalFgm)
            }
        }
        rootView.btMessage.setOnClickListener {
            if (Contents.userId.equals("0")){
                val intent =Intent(activity, WechatLoginAty::class.java)
                activity.startActivityFromFragment(this,intent,1)
            }else{
                context.startActivity(Intent(context,MessageAty::class.java))
            }

        }
    }

    /**
     * 轮播设置
     */
    private fun setSpView() {
        rootView.spView.header=MyHeader()
        rootView.spView.callFresh()
        rootView.spView.setListener(this)
    }
    private fun setPager() {
        pageAdp=SpendVPAdp(context,lunboDatas)
        rootView.viewPager.adapter=pageAdp
        val params = HttpParams()
        lunboPresenter.getLunbo(params)
    }

    override fun showLoading() {
//        loadingDialog.show()
    }

    /**
     * 获取用户信息
     */
    override fun getUser(userInfoBean: UserInfoBean) {
        loadingDialog.dismiss()
        if (userInfoBean.code==200&&context!=null){
            Glide.with(context).load(Urls.BaseUrl+"/"+userInfoBean.data.head_pic).asBitmap().dontAnimate().placeholder(R.mipmap.placeholdericon).into(rootView.btSelf)
        }
    }

    /**
     * 获取广告图
     */
    override fun getADV(advBean: AdvBean) {
        /*if (advBean.code==200){
            datas.clear()
            datas.addAll(advBean.data)
            pageAdp.notifyDataSetChanged()
            if (datas.size!=0){
                LogUtil.log("下表数量为${datas.size}")
                MyUtils.setIndicator(datas,context,rootView.indectorLo,rootView.viewPager)
            }
        }*/
    }

    /**
     * 轮播
     */
    override fun onResp(lunBoBean: LunBoBean) {
        try {
            if (lunBoBean.code==200){
                lunboDatas.clear()
                lunboDatas.addAll(lunBoBean.data)
                pageAdp.notifyDataSetChanged()
                if (lunboDatas.size!=0){
                    MyUtils.setIndicator(lunboDatas,context,rootView.indectorLo,rootView.viewPager)
                }
            }
        }catch (e:Exception){

        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        LogUtil.log("---------------------------来了来了$resultCode")

        if (resultCode!= Activity.RESULT_OK)return
        when(requestCode){
            1->{
                LogUtil.log("---------------------------来了来了")
                val ft = fragmentManager.beginTransaction()
                ft.hide(this)
                ft.commit()
                val mainActivity = activity as MainActivity
                mainActivity.change(2)
            }
        }
    }


}